﻿using System;
using System.Linq;
using System.Web.Mvc;
using RehabilitationMvc.Models;
using RehabilitationMvc.DataAccess;

namespace RehabilitationMvc.Controllers
{
    public class ReviewsController : Controller
    {
        private readonly IRehabilitationRepository _repository;

        //
        // Constructor

        public ReviewsController(IRehabilitationRepository repository)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            _repository = repository;
        }



        //
        // Action Methods


        //
        // GET: /Reviews/

        public ActionResult Index()
        {
            var reviews = _repository.AllReviews().OrderByDescending(r => r.Created).ToList();
            return View(reviews);
        }

        //
        // GET: /Reviews/Details/5

        public ActionResult Details(int id = 0)
        {
            var review = _repository.GetReview(id);
            if (review == null)
            {
                return HttpNotFound();
            }
            return View(review);
        }

        //
        // GET: /Reviews/Create

        public ActionResult Create()
        {
            var review = new Review();
            return View(review);
        }

        //
        // POST: /Reviews/Create

        [HttpPost]
        public ActionResult Create(Review review)
        {
            if (ModelState.IsValid)
            {
                _repository.AddReview(review);
                return RedirectToAction("Index");
            }

            return View(review);
        }

        //
        // GET: /Reviews/Edit/5

        public ActionResult Edit(int id = 0)
        {
            var review = _repository.GetReview(id);
            if (review == null)
            {
                return HttpNotFound();
            }
            return View(review);
        }

        //
        // POST: /Reviews/Edit/5

        [HttpPost]
        public ActionResult Edit(Review review)
        {
            if (ModelState.IsValid)
            {
                _repository.UpdateReview(review);
                return RedirectToAction("Index");
            }
            return View(review);
        }

        //
        // GET: /Reviews/Delete/5

        public ActionResult Delete(int id = 0)
        {
            var review = _repository.GetReview(id);
            if (review == null)
            {
                return HttpNotFound();
            }
            return View(review);
        }

        //
        // POST: /Reviews/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var product = _repository.GetReview(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            _repository.DeleteReview(id);
            return RedirectToAction("Index");
        }
    }
}